package st.business.tepth.business.user;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.business.RootDelegate;
import st.business.tepth.business.bean.LocationBean;
import st.business.tepth.business.event.ReloadLocationEvent;
import st.business.tepth.business.goods.GoodsDelegate;
import st.business.tepth.business.goodscheck.GoodsCheckDelegate;
import st.business.tepth.business.goodsorder.GoodsOrderDelegate;
import st.business.tepth.business.goodsreceive.GoodsReceiveDelegate;
import st.business.tepth.business.history.HistoryDelegate;
import st.business.tepth.business.maintainplan.MaintainPlanDelegate;
import st.business.tepth.business.setting.SettingDelegate;
import st.business.tepth.business.taskallocation.TaskAllocationDelegate;
import st.business.tepth.manager.UserBean;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.base.mvp.MvpFragment;
import st.shadow.library.core.util.utilcode.util.DeviceUtils;
import st.shadow.library.ui.dialog.DialogLoader;
import st.shadow.library.ui.dialog.IDialogClickListener;

/**
 * @author zhangyu
 * @date 2019/3/6
 */
public class UserDelegate extends MvpFragment<UserPresenter> implements UserContract.View {
    public static UserDelegate newInstance() {
        return new UserDelegate();
    }

    @BindView(R2.id.tb_station)
    TabLayout mTabStation;
    @BindView(R2.id.view_pager)
    ViewPager mViewPagerStation;
    @BindView(R2.id.iv_setting)
    ImageView mIvSetting;
    @BindView(R2.id.tv_user_name)
    TextView mTvUserName;
    @BindView(R2.id.tv_allocation)
    TextView mTvAllocation;
    @BindView(R2.id.tv_check)
    TextView mTvCheck;
    @BindView(R2.id.layout_content)
    ConstraintLayout mLayoutContent;
    private DialogLoader mDialogLoader;

    @Override
    public UserPresenter getPresenter() {
        return new UserPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_user;
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        loadData();
    }

    public void loadData() {
        UserBean userBean = UserManager.getInstance().getCurrentUser();
        if (userBean != null) {
            UserBean.BaseInfoBean baseInfoBean = userBean.getBaseInfo();
            if (baseInfoBean != null) {
                mTvUserName.setText(baseInfoBean.getUserName());
            }
        }
        mPresenter.getLocationInfo(DeviceUtils.getAndroidID(),
                UserManager.getInstance().getToken());
        addTopView(R.layout.view_loading, mLayoutContent);
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        EventBus.getDefault().register(this);
        mDialogLoader = new DialogLoader(_mActivity);

        if (!UserManager.getInstance().isGroupLeader()) {
            mTvAllocation.setVisibility(View.GONE);
            mTvCheck.setVisibility(View.GONE);
        }
        mTabStation.setupWithViewPager(mViewPagerStation);
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

    @Subscribe
    public void onReloadDataEvent(ReloadLocationEvent event) {
        mPresenter.getLocationInfo(DeviceUtils.getAndroidID(),
                UserManager.getInstance().getToken());
        addTopView(R.layout.view_loading, mLayoutContent);
    }

    @OnClick(R2.id.tv_logout)
    public void onLogout() {
        if (getParentFragment() != null) {
            ((RootDelegate) getParentFragment()).onLogout();
        }
    }

    @OnClick(R2.id.tv_goods_order)
    public void onGoodsOrder() {
        start(GoodsOrderDelegate.newInstance(0));
    }

    @OnClick(R2.id.tv_wait_pick_goods)
    public void onGoodsReceive() {
        start(GoodsReceiveDelegate.newInstance());
    }

    @OnClick(R2.id.tv_goods)
    public void onGoodsSearch() {
        start(GoodsDelegate.newInstance());
    }

    @OnClick(R2.id.tv_allocation)
    public void onTaskAllocation() {
        start(TaskAllocationDelegate.newInstance());
    }

    @OnClick(R2.id.tv_history)
    public void onHistory() {
        start(HistoryDelegate.newInstance());
    }

    @OnClick(R2.id.tv_check)
    public void onCheck() {
        start(GoodsCheckDelegate.newInstance());
    }

    @OnClick(R2.id.tv_plan)
    public void onPlan() {
        start(MaintainPlanDelegate.newInstance());
    }

    @OnClick(R2.id.iv_setting)
    public void onSetting() {
        start(SettingDelegate.newInstance());
    }

    @Override
    public void showLocationInfo(LocationBean locationBean) {
        removeTopView(mLayoutContent);
        if (locationBean != null) {
            List<LocationBean.FactoryAreaListBean> factoryAreaList
                    = locationBean.getFactoryAreaList();
            if (factoryAreaList != null && !factoryAreaList.isEmpty()) {
                mViewPagerStation.setAdapter(new StationAdapter
                        (getChildFragmentManager(), factoryAreaList));
                mTabStation.removeAllTabs();
                if (factoryAreaList.size() > 6) {
                    mTabStation.setTabMode(TabLayout.MODE_SCROLLABLE);
                } else {
                    mTabStation.setTabMode(TabLayout.MODE_FIXED);
                }
                for (int i = 0, size = factoryAreaList.size(); i < size; i++) {
                    TabLayout.Tab tab = mTabStation.getTabAt(i);
                    if (tab == null) {
                        tab = mTabStation.newTab();
                        mTabStation.addTab(tab);
                    }
                    tab.setCustomView(R.layout.item_tab_station);
                    if (tab.getCustomView() != null) {
                        TextView textView = tab.getCustomView().findViewById(R.id.text);
                        textView.setText(factoryAreaList.get(i).getName());
                        View badge = tab.getCustomView().findViewById(R.id.view_badge);
                        badge.setVisibility(factoryAreaList.get(i).isHaveWork() ?
                                View.VISIBLE : View.INVISIBLE);
                    }
                }
                //mViewPagerStation.setOffscreenPageLimit(factoryAreaList.size());
            } else {
                addTopView(R.layout.view_bus_empty, mLayoutContent);
            }
        } else {
            addTopView(R.layout.view_bus_empty, mLayoutContent);
        }
    }

    @Override
    public void offWorkSucceed() {
        mDialogLoader.stopProgressWithSuccess("下班成功", null, new IDialogClickListener() {
            @Override
            public void onClick() {
                mDialogLoader.clearDialog();
            }
        });
    }

    @Override
    public void showError(String msg) {
        mDialogLoader.stopProgressWithFailed(msg, msg);
    }

    @OnClick(R2.id.tv_off_work)
    public void onOffWork() {
        mPresenter.offWork(UserManager.getInstance().getToken());
        mDialogLoader.showProgressDialog(true, "正在提交", "请稍候...");
    }
}
